#include "ExtraAlg.h"

 
int IterativeBinarySearch(int arr[], int left, int right, int key)
{
	while (left <= right)
	{
		int middle = (left + right) / 2;

		if(key < arr[middle])
		{
			right = middle -1;
		}
		else if(key > arr[middle])
		{
			left = middle + 1;
		}
		else
		{
			return middle;
		}
	}
	return -1;
}

int RecursiveBinarySearch(int arr[], int left, int right, int key)
{
	if(left > right)      //conditie de stop in caz de NOT_FOUND
		return -1;

	int middle = (left + right) / 2;

	if(key < arr[middle])
	{
		 RecursiveBinarySearch ( arr, left, middle - 1, key );
	}
	else if(key > arr[middle])
	{
		 RecursiveBinarySearch( arr, middle + 1, right, key);
	}
	else
	{
		return middle;
	}

	//return -1;  GRESIT !!! se intoarce functia recursiva de rulat si apoi iese cu -1!!!
}